package com.example.logindb.sevices;


import com.example.logindb.entity.Online;
import com.example.logindb.repository.OnlineRepository;

import com.example.logindb.repository.UserRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OnlineServiceImpl implements OnlineService {
    private final UserRepository userRepository;
    private OnlineRepository onlineRepository;
    public OnlineServiceImpl(OnlineRepository onlineRepository, UserRepository userRepository)
    {
        this.onlineRepository = onlineRepository;
        this.userRepository = userRepository;
    }

    @Override
    public void updateStatus(Online online) {
        // 更新在线状态
        onlineRepository.save(online);
    }

    @Override
    public Online findStatusById(Integer id) {
        return onlineRepository.findStatusById(id);
    }

    //弃用
    @Override
    public Integer getOnlineCount() {
        return Math.toIntExact(onlineRepository.count());
    }
    //
    @Override
    public Integer getCountByStatus() {
        Integer online1 = onlineRepository.countByStatus(1);
        Integer online2 = onlineRepository.countByStatus(2);
        Integer online3 = onlineRepository.countByStatus(3);
        return online1 + online2 + online3;
    }

    @Override

    public List<Integer> getOnlineBVCount() {
        List<Online> onlines = onlineRepository.findAllByStatusNot(0);
        List<Integer> countbv = new java.util.ArrayList<>();
        countbv.add(0);
        countbv.add(0);
        if(!onlines.isEmpty()){
            for(Online online:onlines){
                String role = userRepository.findAllById(online.getId()).getUserrole();
                if(role.equals("blind               ")){
                    countbv.set(0,countbv.get(0)+1);
                }else if(role.equals("volunteer           ")){
                    countbv.set(1,countbv.get(1)+1);
                }
            }
        }

        return countbv;
    }
    //0 3 2
    //0 1 2
    //查询离线、等待帮助、帮助中的b人数
    //查询离线、等待帮助、帮助中的v人数
    @Override
    public List<Integer> getOnlineBV() {
        //离线bv
        List<Online> off = onlineRepository.findAllByStatus(0);
        int offb = 0;
        int offv = 0;
        for(Online online:off){
            String role = userRepository.findAllById(online.getId()).getUserrole();
            if(role.equals("blind               ")){
                offb++;
            }else if(role.equals("volunteer           ")){
                offv++;
            }
        }
        //帮助中bv
        List<Online> helping = onlineRepository.findAllByStatus(2);
        int helpingb = 0;
        int helpingv = 0;
        for(Online online:helping){
            String role = userRepository.findAllById(online.getId()).getUserrole();
            if(role.equals("blind               ")){
                helpingb++;
            }else if(role.equals("volunteer           ")){
                helpingv++;
            }
        }
        //待帮助b
        int waitingb = onlineRepository.findAllByStatus(3).size();
        //在线v
        List<Online> online = onlineRepository.findAllByStatus(1);
        int onlinev = 0;
        int onlineb = 0;
        for(Online online1:online){
            String role = userRepository.findAllById(online1.getId()).getUserrole();
            if(role.equals("volunteer           ")){
                onlinev++;
            }
            else if(role.equals("blind               ")){
                onlineb++;
            }
        }
        return List.of(offb,offv,helpingb,helpingv,waitingb,onlinev,onlineb);

    }

    @Override
    public void deleteOnlineById(int id) {
        onlineRepository.deleteById(id);
    }


//    @Override
//    public List<String> findGeojsonByBlindID(int id) {
//        return routeRepository.findGeoJsonByBlindid(id);
//    }


}